package com.hand.qt24953.order.api.controller.v1;

import com.hand.qt24953.order.api.dto.OrderConditionDTO;
import com.hand.qt24953.order.api.dto.OrderDTO;
import com.hand.qt24953.order.config.SwaggerApiConfig;
import com.hand.qt24953.order.domain.entity.SoHeader;
import com.hand.qt24953.order.domain.repository.SoHeaderRepository;
import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hzero.boot.platform.lov.annotation.ProcessLovValue;
import org.hzero.core.base.BaseConstants;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.export.annotation.ExcelExport;
import org.hzero.export.vo.ExportParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;

/**
 * @author qiutao 2019/8/5 14:24
 */
@RestController("orderController.v1")
@RequestMapping("/v1/{organizationId}/orders")
@Api(tags = SwaggerApiConfig.ORDER)
public class OrderController extends BaseController {
    private final SoHeaderRepository soHeaderRepository;
    @Autowired
    public OrderController(SoHeaderRepository soHeaderRepository) {
        this.soHeaderRepository = soHeaderRepository;
    }

    @ApiOperation(value = "销售订单信息列表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @ProcessLovValue(targetField = BaseConstants.FIELD_BODY)
    @GetMapping
    public ResponseEntity<Page<OrderDTO>> list(OrderConditionDTO orderConditionDTO, @ApiIgnore @SortDefault(value = SoHeader.FIELD_SO_HEADER_ID,
            direction = Sort.Direction.DESC) PageRequest pageRequest, @PathVariable Long organizationId) {
        Page<OrderDTO> list = soHeaderRepository.selectByOrderCondition(pageRequest, orderConditionDTO);
        return Results.success(list);
    }

    @GetMapping("/{orderNumber}")
    @ApiOperation(value = "根据订单编号查询订单信息")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @ProcessLovValue(targetField = BaseConstants.FIELD_BODY)
    public ResponseEntity<OrderDTO> detail(@PathVariable Long organizationId, @PathVariable String orderNumber) {
        OrderConditionDTO orderConditionDTO = new OrderConditionDTO();
        orderConditionDTO.setOrderNumber(orderNumber);
        Page<OrderDTO> list = soHeaderRepository.selectByOrderCondition(new PageRequest(0, 1), orderConditionDTO);
        return Results.success(list.isEmpty() ? null : list.get(0));
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出销售订单信息")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @ProcessLovValue(targetField = BaseConstants.FIELD_BODY)
    @ExcelExport(OrderDTO.class)
    public ResponseEntity<Page<OrderDTO>> export(@PathVariable Long organizationId, OrderConditionDTO orderConditionDTO, ExportParam exportParam, HttpServletResponse response, PageRequest pageRequest) {
        Page<OrderDTO> list = soHeaderRepository.export(orderConditionDTO, exportParam, pageRequest);
        return Results.success(list);
    }
}
